from utils.prettyrepr import prettify_class

from cranesched.routing.path import Path


@prettify_class
class Movable1D(object):
    """An object that can move along a single axis. It has an id, speed, size, and a path."""
    def __init__(self, id, time=0.0, position=0.0, speed=1.0, size=1.0):
        self.id = id
        self.size = size
        self.speed = float(speed)
        self.path = Path(time, position)

    def __info__(self):
        time, position = self.path[-1]
        return ("id=%s, time=%s, pos=%s, speed=%s, size=%s" %
                (self.id, time, position, self.speed, self.size))

    def time_to(self, position):
        """Compute the minimum time required for the movable to move from its current position to
        the argument position.
        NOTE: this method does not take into account the possibility of having obstacles and not
        being able to move at maximum speed. It assumes that the crane moves at full speed until
        the target position is reached."""
        return abs(position - self.path.curr_position) / float(self.speed)

    def earliest_arrival_at(self, position):
        """Compute the earliest possible time when the movable can reach 'position', assuming no
        obstacles."""
        return self.path.curr_time + self.time_to(position)
